In [3]:
import numpy as np
import scipy as sp
import sympy
import matplotlib.pyplot as plt
import matplotlib as mpl

In [4]:
# activate pop up plots
#%matplotlib qt
# or change to inline plots
%matplotlib inline

In [5]:
# some sample data
x = np.arange(0,10,0.1)

In [6]:
page_width_cm = 13
dpi = 200
inch = 2.54 # inch in cm
# setting global plot configuration using the RC configuration style
plt.rc('font', family='serif')
plt.rc('xtick', labelsize=12) # tick labels
plt.rc('ytick', labelsize=20) # tick labels
plt.rc('axes', labelsize=20)  # axes labels
# If you don’t need LaTeX, don’t use it. It is slower to plot, and text
# looks just fine without. If you need it, e.g. for symbols, then use it.
#plt.rc('text', usetex=True) #<- P-E: Doesn't work on my Mac

In [7]:
# create a figure instance, note that figure size is given in inches!
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,6))
# set the big title (note aligment relative to figure)
fig.suptitle("suptitle 16, figure alignment", fontsize=16)

# actual plotting
ax.plot(x, x**2, label="label 12")



# set axes title (note aligment relative to axes)
ax.set_title("title 14, axes alignment", fontsize=14)

# axes labels
ax.set_xlabel('xlabel 12')
ax.set_ylabel(r'$y_{\alpha}$ 12', fontsize=8)

# legend
ax.legend(fontsize=12, loc="best")

##############################################################

##Ploting Grid
plt.grid()
#Changing Legend
ax.legend(loc=4)

ax.plot(x, x**2,color="purple", lw=1, ls='-', marker='s',markevery=5, markersize=8,markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue", label="label 12")

#Subplot
#SubPlot
fig, ax1 = plt.subplots(nrows=1, ncols=1, figsize=(8,6))
ax1.plot(x, x**3, label="label 12")
ax1.set_title("Sub-plotting", fontsize=14)




# saving the figure in different formats
fig.savefig('figure-%03i.png' % dpi, dpi=dpi)
fig.savefig('figure.svg')
fig.savefig('figure.eps')



In [8]:
### Countour Plotting

import math
from pylab import *

alpha = 0.7
phi_ext = 2 * math.pi * 0.5

def flux_qubit_potential(phi_m, phi_p):
    return 2 + alpha - 2 * np.cos(phi_p)*np.cos(phi_m) - alpha * np.cos(phi_ext - 2*phi_p)

phi_m = np.linspace(0, 2*math.pi, 100)
phi_p = np.linspace(0, 2*math.pi, 100)
X,Y = np.meshgrid(phi_p, phi_m)
Z = flux_qubit_potential(X, Y).T


fig, ax = plt.subplots()

cnt = contour(Z, cmap=cm.RdBu, vmin=abs(Z).min(), vmax=abs(Z).max(), extent=[0, 1, 0, 1])



# Second Axis
ax2 = ax.twinx()

ax2.set_ylabel(r"Temperature ($^\circ$C)")
ax2.set_ylim(0, 35)



#Add Line
axvline(x=0.5, ymin=0, ymax=1)
axhline(y=25,xmin=0,xmax=1)


#autoformat dates for nice printing on the x-axis using fig.autofmt_xdate()
fig.autofmt_xdate(bottom=0.2, rotation=30, ha='right')


Advanced exercises

We are going to play a bit with regression -Create a vector x of equally spaced number between x∈[0,5π] of 1000 points (keyword: linspace) -create a vector y, so that y=sin(x) with some random noise -plot it -Try to do a polynomial fit on y(x) using numpy.polyfit, with different polynomial degree

In [16]:
x= np.linspace(0, 5*math.pi, 1000)

Y= np.sin(x)
noise = np.random.normal(-0.1,0.1,1000)

y=Y+noise

fig, ax1 = plt.subplots(nrows=1, ncols=1, figsize=(8,6))
fig.suptitle("Sin + Noise", fontsize=16)
ax1.plot(x, y,color="black",lw=2, ls='*', marker='.', label="Sin(x) + Noise")



plt.plot(x,Y,color="red", linewidth=2.00,label="Sin(x)")

for i in [2,4,6,8,10]:
    p = np.poly1d(np.polyfit(x,y,i))
    plt.plot(x,p(x), linewidth=2.00,label="Fit order: "+str(i))
    

ax1.legend(fontsize=12, loc="best")


Out[16]:
<matplotlib.legend.Legend at 0x83f1c30>

In [9]: